package com.benbi.shequapi.controller;

import com.benbi.shequapi.utils.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

@Controller
@RequestMapping("/test")
public class TestController {

    @ResponseBody
    @RequestMapping("/testImageUpload")
    public Result testImageUpload(@RequestParam("file") MultipartFile file) {
        try{
            File saveFile = new File("D:/testUpload.png");
            OutputStream os = new FileOutputStream(saveFile);

            int len = 1024;
            byte[] buffer = new byte[len];

            InputStream is = file.getInputStream();
            while ((len = is.read(buffer, 0, len)) != -1){
                os.write(buffer,0, len);
            }

            os.close();

        }catch (Exception e){
            e.printStackTrace();
        }

        return new Result("上传成功");
    }
}
